package com.shgsh.attribute.repository;

import com.lovver.ssdbj.core.BaseResultSet;
import com.lovver.ssdbj.exception.SSDBException;
import com.lovver.ssdbj.pool.SSDBDataSource;
import com.lovver.ssdbj.pool.SSDBPoolConnection;
import com.shgsh.common.repository.BasicRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * Created by pcc on 2015/8/4.
 */
@Repository("testRepository")
public class TestRepository{
    Logger logger = LoggerFactory.getLogger(BasicRepository.class);


    @Autowired
    SSDBDataSource ssdbDataSource;
    SSDBPoolConnection conn = null;


    /**
     * 数据库建立连接*/

    public SSDBPoolConnection init(){
        try {
            conn = ssdbDataSource.getConnection();
            logger.debug("database connect success.");
            return conn;
        } catch (Exception e) {
            System.out.println("exception : 连接失败！ " + conn);
            e.printStackTrace();
        }
        return null;
    }


    /**
     * KV操作
     * 键--值
     * 说明：键值对存储，新值会覆盖
     * multi_set
     * 多个KEY-VALUE输入
     * 例子：ArrayList mset_params = new ArrayList();
     mset_params.add("key1".getBytes());
     mset_params.add("hahahahaha".getBytes());
     mset_params.add("key2".getBytes());
     mset_params.add("lalalalala".getBytes());
     conn.executeUpdate("multi_set", mset_params);*/

    public void mset(ArrayList<byte[]> mset_params){

        conn = init();
        if(conn != null)
        try {
            conn.executeUpdate("multi_set", mset_params);
            logger.debug("多个KEY-VALUE输入,操作成功。");
        } catch (SSDBException e) {
            logger.debug("KV操作失败");
            e.printStackTrace();
        }
    }
    /**
     * multi_get
     * 多个VALUE获取
     * ArrayList mget_params = new ArrayList();
     * mget_params.add("key1".getBytes());
     * mget_params.add("key2".getBytes());*/

    public Map<String,String> mget(ArrayList<byte[]> mget_params){
        conn = init();
        if(conn != null)
        try {
            BaseResultSet<Map<byte[], byte[]>> m_rs = conn.execute("multi_get",
                    mget_params);
            Map<byte[], byte[]> items = m_rs.getResult();
            Map<String,String> returnData = new HashMap();
            Iterator<byte[]> ite = items.keySet().iterator();
            while (ite.hasNext()) {
                byte[] key = ite.next();
                returnData.put(key.toString(), items.get(key).toString());
            }
            return returnData;
        } catch (SSDBException e) {
            e.printStackTrace();
        }
        return null;
    }



    @SuppressWarnings({"rawtypes", "unchecked"})
    public void CURD() throws Exception {
        try {





            /**
     * scan
     * 列出K-V列表*/

            System.out.println("\nscan列表：\n");
            ArrayList<byte[]> scan_params = new ArrayList();
            scan_params.add("".getBytes());
            scan_params.add("".getBytes());
            scan_params.add("10".getBytes());
			/*执行命令*/
            BaseResultSet<Map<byte[], byte[]>> scan_rs = conn.execute("scan",
                    scan_params);
            Map<byte[], byte[]> scan_items = scan_rs.getResult();
			/*分析结果*/
            Iterator<byte[]> scan_ite = scan_items.keySet().iterator();
            while (scan_ite.hasNext()) {
                byte[] key = scan_ite.next();
                System.out.println(new String(key) + "\t\t\t\t\t====="
                        + new String(scan_items.get(key)));
            }

            /***************************************************************************************************************
            *
     * Hmap操作
     * 表名--键--值
     * 说明：多格式哈希表，新值会覆盖

            *
     * hset*/

            ArrayList paramsset = new ArrayList();
            paramsset.add("mapname".getBytes());
            paramsset.add("key".getBytes());
            paramsset.add("value".getBytes());

            paramsset.add("key1");
            paramsset.add("value1");
            conn.executeUpdate("hset", paramsset);
            /**
     * hget*/

            ArrayList params = new ArrayList();
            params.add("mapname".getBytes());
            params.add("key".getBytes());
            BaseResultSet<byte[]> rs = conn.execute("hget", params);
            System.out.print("\nvalue:");
            System.out.println(new String(rs.getResult()));


            /***************************************************************************************************************

            **/
     /** zmap
     * 表名--键--值
     * 说明：自动排序数据，新值会覆盖

            *
     * zset*/

            ArrayList paramszset = new ArrayList();
            paramszset.add("zmapname".getBytes());
            paramszset.add("keyname".getBytes());
            paramszset.add("110".getBytes());
            conn.executeUpdate("zset", paramszset);


            /**
     * zget*/

            ArrayList paramszget = new ArrayList();
            paramszget.add("zmapname".getBytes());
            paramszget.add("keyname".getBytes());
            BaseResultSet<byte[]> rzget = conn.execute("zget", paramszget);
            System.out.print("\nzget:");
			/*返回类型为long*/
            System.out.println("" + rzget.getResult());

            /***************************************************************************************************************
            *
     * Qstack操作
     * 存取与上类似存为conn.executeUpdate，取为conn.execute
     * 说明：队列操作
     */

        } finally {
            conn.close();
        }
    }
}
